package com.iyundao.io.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.iyundao.io.Service.SysRoleService;
import com.iyundao.io.Service.SysUserService;
import com.iyundao.io.model.SysUser;
import com.iyundao.io.tools.ResponseData;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * Created by black on 2019/3/20.
 */
@RestController
@RequestMapping(value = "/api/user")
public class UserController {

    @Autowired
    SysUserService SysUserService;

    @Autowired
    SysRoleService sysRoleService;

    @ApiOperation(value="用户新增", notes="用户新增")
    @RequestMapping(value = "/newUserInfo", method = {RequestMethod.POST,RequestMethod.GET}, produces = {"application/json;charset=UTF-8"})
    public SysUser newUserInfo(@RequestBody JSONObject params){
        SysUser sysUser = JSON.toJavaObject(params,SysUser.class);
        String username = sysUser.getLoginName();
        for(int i = 0; i < 1000000; i++){
            sysUser.setLoginName(username+i);
            SysUserService.addSysUser(sysUser);
        }
        return SysUserService.addSysUser(sysUser);
    }

    @ApiOperation(value="批量插入用户", notes="批量插入用户")
    @RequestMapping(value = "/batchNewUserInfo", method = {RequestMethod.POST,RequestMethod.GET}, produces = {"application/json;charset=UTF-8"})
    public int batchNewUserInfo(@RequestBody JSONObject params){
        SysUser sysUser = JSON.toJavaObject(params,SysUser.class);
        String username = sysUser.getLoginName();

        List<SysUser> tempSysUser = new LinkedList<>();
        for(int i = 0; i < 100000; i++){
            sysUser.setLoginName(username+i);
            tempSysUser.add(sysUser);
        }

        List<SysUser> tempSysUser1 = new ArrayList<>();
        for(int i = 0; i < 100000; i++){
            sysUser.setLoginName(username+i);
            tempSysUser1.add(sysUser);
        }

        long startTime1=System.currentTimeMillis();   //获取开始时间
//        int i = SysUserService.batchAddSysUser(tempSysUser);
        long endTime1=System.currentTimeMillis(); //获取结束时间

        System.out.println("程序运行时间： "+(endTime1-startTime1)+"ms");

        return 0;
    }

//    currentUser

    @ApiOperation(value="用户登录查询", notes="用户登录查询")
    @RequestMapping(value = "/searchCurrentUserUserInfo", method = {RequestMethod.POST,RequestMethod.GET}, produces = {"application/json;charset=UTF-8"})
    public ResponseData searchCurrentUserUserInfo(){
        return new ResponseData(200,"获取成功",SecurityUtils.getSubject().getPrincipal());
    }

    @ApiOperation(value="未登录", notes="用户未登录返回")
    @RequestMapping(value = "/noLogin", method = {RequestMethod.POST,RequestMethod.GET}, produces = {"application/json;charset=UTF-8"})
    public ResponseData noLogin() {
        return new ResponseData(199,"未登录");
    }

    @ApiOperation(value="退出登录", notes="退出登录")
    @RequestMapping(value = "/outLogin", method = {RequestMethod.POST,RequestMethod.GET}, produces = {"application/json;charset=UTF-8"})
    public ResponseData outLogin() {
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return new ResponseData(199,"未登录");
    }

    @ApiOperation(value="没权限", notes="用户没权限")
    @RequestMapping(value = "/noPermission", method = {RequestMethod.POST,RequestMethod.GET}, produces = {"application/json;charset=UTF-8"})
    public ResponseData noPermission() {
        return new ResponseData(198,"没权限");
    }

    @ApiOperation(value="用户登录查询", notes="用户登录查询")
    @RequestMapping(value = "/searchUserInfo", method = {RequestMethod.POST,RequestMethod.GET}, produces = {"application/json;charset=UTF-8"})
    public ResponseData searchUserInfo(@RequestBody JSONObject params){

        SysUser sysUser = JSON.toJavaObject(params,SysUser.class);

        // 从SecurityUtils里边创建一个 subject
        Subject subject = SecurityUtils.getSubject();
        // 在认证提交前准备 token（令牌）
        UsernamePasswordToken token = new UsernamePasswordToken(sysUser.getLoginName(), sysUser.getLoginPwd());
        // 执行认证登陆
        try{
            subject.login(token);
            return new ResponseData(200, "登录成功",subject.getSession().getId().toString());
        }catch (Exception ex){
            if(ex.getMessage().equals("101")){
                return new ResponseData(101, "用户不存在！");
            }else if(ex.getMessage().equals("102")){
                return new ResponseData(102, "密码错误！");
            }else if(ex.getMessage().equals("110")){
                return new ResponseData(110, "账户已冻结！");
            }else{
                ex.printStackTrace();
                return new ResponseData(0, ex.getMessage());
            }
        }
    }
}
